﻿using MediatR;
using SuYuanSystem.Applications;
using SuYuanSystem.Domain;
using SuYuanSystem.Repositories;
using SuYuanSystem.WebApi.MedialRs.Commands.CultivationInfos;

namespace SuYuanSystem.WebApi.MedialRs.CommandHandlers.CultivationInfos
{
    public class CultivationInfoQueryCommandsHandler : IRequestHandler<CultivationInfoQueryCommands, ApiResult<CultivationInfo>>
    {
        public CultivationInfoQueryCommandsHandler(IRepository<CultivationInfo> repository)
        {
            Repository = repository;
        }

        public IRepository<CultivationInfo> Repository { get; }

        /// <summary>
        /// 处理
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        public Task<ApiResult<CultivationInfo>> Handle(CultivationInfoQueryCommands request, CancellationToken cancellationToken)
        {
            //异常处理
            try
            {
                ApiResult<CultivationInfo> result = new ApiResult<CultivationInfo>();
                result.statusCode = StatusCode.Success;
                result.Message = "查询成功";
                var list = Repository.GetAll();
                var entity = list.Result.FirstOrDefault();
                result.Data = entity;
                return Task.FromResult(result);
            }
            catch (Exception)
            {

                throw;
            }
        }
    }
}
